import {
  Http
} from "../../utils/http"
import {
  Tv
} from "../../model/Tv"
import {
  config
} from "../../config/config"
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import {
  User as bb
} from "../../model/user";

Page({

  data: {
    tvList: [],
    show: true,
    loading: false,
    active: 1,
    gourpList: [],
    https: ''
  },

  onLoad: async function () {
    let http = await Http.login() //登录获取token
    let u = await bb.getUserInfo()
    this.setData({
      https: config.apiBaseUrl,
    })
    //盒子列表
    await this.getTvlist()
    await this.getGroupList()
  },

  //获取TV盒子列表
  getTvlist: async function () {
    let tv = await Tv.get_tvbox_list()
    if (tv.code == 0) {
      this.setData({
        tvList: tv.data
      })
    } else {
      console.log(tv.msg);
    }
  },

  //删除盒子接口实现
  async del(ids) {
    let del = await Tv.del_tvbox_detail(ids);
    console.log(del);
    if (del.code == 0) {
      setTimeout(() => {
        wx.showToast({
          title: '删除成功',
          icon: 'success'
        })
      }, 1000);
    } else {
      console.log(del.code);
    }
  },

  //删除按钮
  removeVideoTap() {
    if (this.data.tvList != '' || this.data.gourpList != '') {
      this.setData({
        show: !this.data.show
      })
    } else {
      wx.showToast({
        title: '没有任何盒子',
        icon: 'error'
      })
    }
  },

  //点击删除视频小按钮
  onChangetv(e) {
    console.log(e);
    let that = this;
    let list = that.data.tvList;
    if (that.data.tvList != '') {
      const beforeClose = (action) => new Promise((resolve) => {
        setTimeout(() => {
          if (action === 'confirm') {
            for (let i = 0; i < list.length; i++) {
              if (list[i].id == e.currentTarget.dataset.index) {
                that.del(list[i].id)
                that.onRefresh()
              }
            }
            resolve(true);
          } else {
            // 拦截取消操作
            resolve(true);
          }
        }, 1000);
      });

      Dialog.confirm({
        title: '删除',
        message: '确认删除吗？',
        beforeClose
      });
    } else {
      wx.showToast({
        title: '没有任何盒子',
        icon: 'error'
      })
    }
  },

  //点击删除组小按钮
  onChange(e) {
    let that = this;
    let list = that.data.gourpList.data;
    console.log(list);
    if (that.data.gourpList.data != '') {
      const beforeClose = (action) => new Promise((resolve) => {
        setTimeout(() => {
          if (action === 'confirm') {
            for (let i = 0; i < list.length; i++) {
              if (list[i].id == e.currentTarget.dataset.index) {
                that.del_video_group(list[i].id)
                that.onRefresh()
              }
            }
            resolve(true);
          } else {
            // 拦截取消操作
            resolve(true);
          }
        }, 1000);
      });

      Dialog.confirm({
        title: '删除',
        message: '确认删除吗？',
        beforeClose
      });
    } else {
      wx.showToast({
        title: '没有任何组',
        icon: 'error'
      })
    }
  },

  //组-列表接口实现
  async getGroupList() {
    let tv = await Tv.get_video_group_list();
    console.log(tv);
    if (tv.code == 0) {
      this.setData({
        gourpList: tv
      })
    } else {
      console.log(tv.msg);
    }
  },

  //组-删除接口实现
  async del_video_group(id) {
    let tv = await Tv.del_video_group(id);
    console.log(tv);
  },

  //下拉刷新
  async onRefresh() {
    //if ==undefined 为组的下拉事件
    this.setData({
      loading: true
    })
    this.getGroupList()
    this.getTvlist()
    setTimeout(() => {
      this.setData({
        loading: false
      })
    }, 500);
  },

  //扫描二维码
  scanCode: function () {
    var that = this;
    wx.scanCode({
      success: (res) => {
        console.log("扫码结果");
        console.log(res);
        const scene = decodeURIComponent(res.path.split("=")[1]);
        console.log(scene);

        wx.navigateTo({
          url: "/" + res.path,
        })
      },
      fail: (res) => {
        wx.showToast({
          title: 'fail',
          icon: 'none',
          duration: 1000
        })
        console.log(res);
      }
    })
  },

})